package com.sky.controller.admin;

import com.sky.Service.CategoryService;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.result.PageResult;
import com.sky.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

@Slf4j
@Api(tags = "分类相关接口")
@RestController(value = "adminCategoryController")
@RequestMapping("/admin/category")
public class CategoryController {
    @Autowired
    public CategoryService categoryService;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 获取分类列表
     *
     * @param categoryPageQueryDTO
     * @return
     */
    @ApiOperation("获取分类列表")
    @GetMapping("/page")
    public Result<PageResult> getByPage(CategoryPageQueryDTO categoryPageQueryDTO) {
        System.out.println("categoryPageQueryDTO = " + categoryPageQueryDTO);
        //先尝试从缓存中获取数据
        String cacheKey = generateCacheKey(categoryPageQueryDTO);
        PageResult cachedResult = (PageResult) redisTemplate.opsForValue().get(cacheKey);
        //如果缓存中有数据，则直接返回缓存中的数据
        if (cachedResult != null) {
            return Result.success(cachedResult);
        }
        //如果缓存中没有数据，则查询数据库，并将查询结果缓存到redis中
        PageResult byPage = categoryService.getByPage(categoryPageQueryDTO);
        redisTemplate.opsForValue().set(cacheKey, byPage);
        return Result.success(byPage);
    }

    /**
     * 添加分类
     *
     * @param categoryDTO
     * @return
     */
    @ApiOperation("添加分类")
    @PostMapping
    public Result<String> addDishCategory(@RequestBody CategoryDTO categoryDTO) {
        System.out.println("categoryDTO = " + categoryDTO);
        //清除缓存
        String key = "category_page_*";
        clearCache(key);
        categoryService.addDishCategory(categoryDTO);
        return Result.success("成功添加分类信息");
    }

    /**
     * 更新分类状态
     *
     * @param status
     * @param id
     * @return
     */
    @ApiOperation("更新分类状态")
    @PostMapping("/status/{status}")
    public Result<String> updateCategoryStatus(@PathVariable Integer status, Long id) {
        System.out.println("status = " + status);
        System.out.println("id = " + id);
        //清除缓存
        String key = "category_page_*";
        clearCache(key);
        categoryService.updateCategoryStatus(status, id);
        return Result.success("成功更新分类状态");
    }

    /**
     * 修改分类信息
     *
     * @param categoryDTO
     * @return
     */
    @ApiOperation("修改分类信息")
    @PutMapping
    public Result<String> modifyCategory(@RequestBody CategoryDTO categoryDTO) {
        System.out.println("categoryDTO = " + categoryDTO);
        //清除缓存
        String key = "category_page_*";
        clearCache(key);
        categoryService.modifyCategory(categoryDTO);
        return Result.success("成功修改分类信息");
    }

    /**
     * 删除分类信息
     *
     * @param id
     * @return
     */
    @ApiOperation("删除分类信息")
    @DeleteMapping
    public Result<String> deleteByCategoryId(Long id) {
        System.out.println("id = " + id);
        //清除缓存
        String key = "category_page_*";
        clearCache(key);
        categoryService.deleteById(id);
        return Result.success("成功删除分类信息");
    }

    /**
     * 根据类型查询分类列表
     *
     * @param type
     * @return
     */
    @ApiOperation("根据类型查询分类列表")
    @GetMapping("/list")
    public Result<List<Category>> getCategoryListByType(Integer type) {
        System.out.println("type = " + type);
        List<Category> categoryListByType = categoryService.getCategoryListByType(type);
        log.info("获取分类列表成功");
        return Result.success(categoryListByType);
    }

    // 生成缓存键的方法，根据查询条件生成唯一的缓存键
    private String generateCacheKey(CategoryPageQueryDTO categoryPageQueryDTO) {
        return "category_page_" + categoryPageQueryDTO.getPage() + "_" + categoryPageQueryDTO.getPageSize();
    }

    /**
     * 清除缓存
     */
    private void clearCache(String pattern) {
        Set keys = redisTemplate.keys(pattern);
        redisTemplate.delete(keys);
    }

}
